# -*- coding: utf-8 -*-
import os
import time
from pywinauto.keyboard import send_keys  # 导入按键操作库
from uiautomation import *  # 界面自动化库
import requests
# 随机
url = "http://open.iciba.com/dsapi/"
r = requests.get(url)
content = r.json()['content']
note = r.json()['note']
print(content)
print(note)
class wechat_auto:
    """
        初始化函数,依次传入微信的地址, 目标人物微信名, 循环次数(默认为3次), 循环间隔时间(默认5s)
    """

    def __init__(self, wechat_address, person_name):
        self.wechat_address = wechat_address
        self.person_name = person_name
        self.open_app(wechat_address)
        time.sleep(3)  # 等3秒等待软件加载出来

    def open_app(self, app_dir):
        os.startfile(app_dir)

    def page_open(self):
        # 进入微信，模拟搜索按键: Ctrl+F
        send_keys('^f')  # 调用按键输入
        send_keys(self.person_name)
        time.sleep(1)  # 等待搜索到
        send_keys('{ENTER}')  # 回车键必须全部大小
        time.sleep(1)  # 等待两秒打开目标任务对话界面

        self.wechat_window = WindowControl(searchDepth=1, ClassName="WeChatMainWndForPC")
        self.wechat_window.SetActive()
        time.sleep(1)  # 等待界面稳定

    def send_message(self, msg):

        # 输入聊天内容+ note + content
        send_keys(msg)

        # 回车发送消息
        send_keys('{ENTER}')

        time.sleep(1)


    """
        微信呼叫函数
    """



    # cycle_times如果设为-1,则为持续发送
    def call(self, cycle_times=-1, delay_time=5, wait_time=37):
        i = 0
        while cycle_times == -1 or i < cycle_times:
            self.page_open()  # 跳转到目标人物的聊天界面,将其放置在桌面页面最前端
            # 在微信聊天界面中查找语音通话按钮
            voice_call_button = self.wechat_window.ButtonControl(Name="语音聊天")
            if voice_call_button.Exists():
                voice_call_button.Click()
                time.sleep(wait_time)  # 如果拨通，等待一定时间
            else:
                print("未找到语音通话按钮")

            # 对方没有接听，直接取消通话
            # 界面跳转到语音聊天框
            voice_call_window = WindowControl(searchDepth=1, ClassName="AudioWnd")
            voice_call_button = voice_call_window.ButtonControl(Name="取消")
            if voice_call_button.Exists():
                voice_call_button.Click()
            else:
                print("未找到取消通话按钮")

            time.sleep(1)

            # 对方接听，直接挂断\
            # 界面跳转到语音聊天框
            voice_call_window = WindowControl(searchDepth=1, ClassName="AudioWnd")
            voice_call_button = voice_call_window.ButtonControl(Name="挂断")
            if voice_call_button.Exists():
                voice_call_button.Click()
            else:
                print("未找到挂断通话按钮")

            time.sleep(delay_time)
            i = i + 1

        # exit()  # 退出程序

    def send_words(self, words, cycle_times=-1, delay_time=5):  # cycle_times如果设为-1,则为持续发送
        i = 0
        while cycle_times == -1 or i < cycle_times:
            self.page_open()  # 跳转到目标人物的聊天界面,将其放置在桌面页面最前端

            # 需要发送的消息内容
            for j in range(len(words)):
                self.send_message(words[j])

            time.sleep(delay_time)
            i = i + 1

        # exit()  # 退出程序
def hide_window(window):
    window.SetActive()
    send_keys('{F6}')
'''
    主程序入口
'''
if __name__ == "__main__":
    app_dir = r'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\微信'  # 此处为微信的绝对路径

    # 依次传入微信的地址, 目标人物微信名, 循环次数(默认为3次), 循环间隔时间(默认5s), 通话等待时间
    auto_send = wechat_auto(app_dir, "B—赵焱")

    """
        @Test1
    """
    # # 呼叫老婆, 循环呼叫5次, 间隔2秒
    # auto_send.call(cycle_times=5, delay_time=2, wait_time=37)

    """
        @Test2
    """
    # 待发送文字列表,以逗号分隔 传入note
    word_to_send = ["请认真和我讲话~~~~，谢谢！", "请认真和我讲话~~~~，谢谢！", "请认真和我讲话~~~~，谢谢！", "请认真和我讲话~~~~，谢谢！", "请认真和我讲话~~~~，谢谢！"]
    auto_send.send_words(word_to_send, cycle_times=5, delay_time=3)

# 可以隐藏掉窗口继续发送
